package com.zrx.govern.component.easyexcel.starter.validate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.zrx.govern.component.easyexcel.starter.feign.entity.DicTreeDto;
import com.zrx.govern.component.easyexcel.starter.feign.IDictionaryFeignClientService;
import com.zrx.govern.component.easyexcel.starter.utils.JsonParseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 字典获取
 *
 * @author zhangxiaowei
 */
@Component
public class DicAccessor {
    @Autowired
    private IDictionaryFeignClientService iDictionaryFeignClientService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public static final String DIC_CODE_CACHE = "DIC_DATA:MODEL:";

    /**
     * 获取model下所有字典值
     *
     * @param model
     * @return dicMap
     */
    public Map<String, List<String>> transferDictValue(String model) {
        Map<String, List<String>> dicMap = new HashMap<>(6);
        String nameToCode = stringRedisTemplate.opsForValue().get(DIC_CODE_CACHE + model);
        if (StringUtils.isNotBlank(nameToCode)) {
            return JSON.parseObject(nameToCode, new TypeReference<Map<String, List<String>>>() {
            });
        }
        Map<Integer, String> dictionaries = JsonParseUtil.getDictionaries(model);
        List<String> codes = new ArrayList<>(dictionaries.values());
        List<DicTreeDto> dicTreeDtoList = iDictionaryFeignClientService.getDictionaryValByCodes(codes).getData();
        dicTreeDtoList.forEach(dicTreeDto -> {
            String code = dicTreeDto.getCode();
            List<Map<String, Object>> options = dicTreeDto.getOptions();
            JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(options));
            List<String> list = new ArrayList<>();
            parseArray(jsonArray, null, list);
            dicMap.put(code, list);
        });
        stringRedisTemplate.opsForValue().set(DIC_CODE_CACHE + model, JSON.toJSONString(dicMap), 3, TimeUnit.DAYS);
        return dicMap;
    }

    /**
     * 递归获取所有字典值
     *  @param array
     * @param parent
     * @param list
     */
    public void parseArray(JSONArray array, JSONObject parent, List<String> list) {
        for (Object ob : array) {
            JSONObject o = (JSONObject) ob;
            String label = "";
            if (Objects.nonNull(parent)) {
                String parentLabel = parent.getString("parentLabel");
                label = parentLabel + "/" + o.getString("label");
            } else {
                label = o.getString("label");
            }
            if (o.getJSONArray("children") != null) {
                o.put("parentLabel", label);
                parseArray(o.getJSONArray("children"), o, list);
            } else {
                list.add(label);
            }
        }
    }
}
